<template>
	<view class="container">
		<view class="top">
			<view class="user-info">
				<text>用户：{{user}}</text>
				<text>剩余积分：{{points}}</text>
			</view>
		</view>

		<view class="exchange" v-for="(item,index) in goods" :key="index" :class="index == 0 ? 'stynew':' '">
			<view class="exchange-goods" @click="Togoodsinfo(item)">
				<image :src="item.cover_img" mode="aspectFill"></image>
				<view class="goods-text">
					<text>{{item.title}}</text>
				</view>
			</view>
			<view class="but">
				<text>积分：{{item.goods_price}}</text>
				<button @click="postorder(item)">兑换</button>
			</view>

		</view>

		<view class="" style="text-align: center;padding-bottom: 30upx;">
			<text style="color: #595959;font-size: 30upx;">暂时没有更多数据了...</text>
		</view>

		<!-- 分享 -->
		<share ref="share" :contentHeight="580" :shareList="shareList"></share>
	</view>
</template>

<script>
	import share from '@/components/share';
	import uniIcons from '../../ed-icons/ed-icons.vue'
	import {
		getOne,
		postOne
	} from '../../api/publicapi.js'
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue'

	var _self, page = 1;
	export default {
		components: {
			share,
			uniIcons,
			//注册组件        
			uniLoadMore
		},
		data() {
			return {
				goods: [],
				points: 0,
				user: [],
				goodsid:[],
                addressData:{}
			}
		},
		async onLoad(options) {
            this.getaddressdata()
			this.getgoods()
            
		},
		methods: {
            
            getaddressdata(){
                  let addressList = uni.getStorageSync('addresslist')
                  console.log(addressList)
                  if(addressList==''){
                      uni.showModal({
                          title: '提示',
                          content: '您尚未填写地址请先填写地址',
                          success: function (res) {
                             let url = '/pages/address/address'      
                              uni.navigateTo({  
                              	url
                              })
                          }
                      })
                  } 
                  for (var i = 0; i < addressList.length; i++) {
                      if(addressList[i].default == true){
                          this.addressData = addressList[i]
                          return
                      }
                      
                  }
            },
			getgoods() {
				getOne('front/cms/getbargain').then(res => {
                    if(res.data.response!=undefined)return
					this.goods = res.data.shop
					this.points = res.data.points
					this.user = res.data.user.nickname
				})
			},
            
			postorder(item) {
               console.log(this.addressData)
				postOne('front/cms/postexchange/' + item.id,this.addressData).then(res => {
					
						uni.showModal({
							content: res.data.response,
							showCancel: false
						})
					
				})
			},

			// 店铺详情
			navToDetailPage(item) {
				let id = item.id;
				uni.navigateTo({
					url: `/pages/product/product?id=${id}`
				})
			},

			// 商品详情
			Togoodsinfo(val) {
				let id = val.id;
				uni.navigateTo({
					url: `/pages/product/product?id=${id}`
				})
			},

			//分享
			share() {
				this.$refs.share.toggleMask();
			},
			stopPrevent() {}
		},

	}
</script>

<style lang='scss'>
	page {
		background: #f7f7f7;
	}

	.top {
		width: 100%;
		border-bottom: 1px solid #dfdfdf;
		height: 80upx;
		position: fixed;
		top: 0;
		background: #f7f7f7;

		.user-info {
			display: flex;
			justify-content: space-between;
			font-size: 30upx;
			width: 92%;
			margin: 0 auto;
		}

		text {
			padding-top: 20upx;
		}
	}

	.stynew {
		margin-top: 81upx;
	}

	.exchange {
		background: #fff;
		margin-bottom: 30upx;
		padding: 40upx 0;

		.exchange-goods {
			width: 92%;
			margin: 0 auto;

			image {
				width: 100%;
				height: 350upx;
				border-radius: 15upx;
			}

			.goods-text {
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
				width: 95%;
				margin: 0 auto;
				padding-top: 17upx;
				color: #595959;

				text {
					font-size: 30upx;
				}
			}
		}

		.but {
			width: 88%;
			display: flex;
			justify-content: space-between;
			margin: 0 auto;
			padding-top: 31upx;

			text {
				color: #d23931;
				font-size: 34rpx;
			}

			button {
				margin: 0;
				width: 195upx;
				height: 59upx;
				line-height: 58upx;
				font-size: 28upx;
				background: #d23931;
				color: #fff;
			}
		}
	}
</style>
